'''
Created on Jul 10, 2009

@author: Roni
'''
import unittest
from cliques.potentialCliques import potentialCliques
from cliques.mdp.heuristicMdp import extendedState
from cliques.kPotentialClique import kPotentialClique
import graph
class unittest_extendedState(unittest.TestCase):

    def testExtend3(self):
        k=4
        pcdb = [kPotentialClique([0,6], [8,2]),kPotentialClique([6],[5,9,2,3,8,4])]
        potentials = potentialCliques(k, pcdb)
        new_potentials = potentials.expand(8,[5,4])
        assert len(new_potentials.pcdb)==2
        expected_kPcs = [kPotentialClique([6],[5,9,2,3,4]),\
                         kPotentialClique([6,8], [4,5])]
        for kPc in expected_kPcs:
            assert self.contains_kPc(new_potentials, kPc)
        

    def testExtend2(self):
        k=4
        pcdb = [kPotentialClique([5], [1,2,3,4,0])]
        potentials = potentialCliques(k, pcdb)
        new_potentials = potentials.expand(1, [3,4])
               
        assert len(new_potentials.pcdb)==2
        expected_kPcs = [kPotentialClique([5], [2,3,4,0]),\
                         kPotentialClique([5,1], [3,4])]
        for kPc in expected_kPcs:
            assert self.contains_kPc(new_potentials, kPc)
                    
    def contains_kPc(self,potentials, searched_kPc):
        ''' Check if searched_kPc is in potentials (potentialCliques object) '''
        for kPc in potentials.pcdb:
            if kPc.same(searched_kPc):
                return True
        return False
                

    def testExtend(self):
        ckg = graph.graph()
        nodes = [1,2,3,4,5,6,7,8]
        ckg.add_nodes(nodes)
        ckg.add_edge(1,2)
        ckg.add_edge(2,7)
        ckg.add_edge(7,1)
        ckg.add_edge(8,1)
        ckg.add_edge(6,1)
        ckg.add_edge(2,1)
        ckg.add_edge(1,8)
        ckg.add_edge(1,6)
        ckg.add_edge(1,7)
        ckg.add_edge(7,2)
        generated = [7,2]
        
        kPc = kPotentialClique([1], [6,2,7])
        pcdb = potentialCliques(3,[kPc])
        state_0 = extendedState(pcdb,0)
        
        new_pcdb = pcdb.expand(2, [7])
        assert new_pcdb.max_core==2
#        
#        
#        potentialCliques(3, pcdb)
#        "state" = extendedState: Gen[[7]],Ckg[<graph object [8, 1, 6, 2, 7] 
#        [(1, 2), (2, 7), (7, 1), (8, 1), (6, 1), (2, 1), (1, 8), (1, 6), (1, 7), (7, 2)]>]    
#        "parent"= extendedState: Gen[[7, 2]],Ckg[<graph object [8, 1, 6, 2, 7] [(1, 2), (7, 1), (8, 1), (6, 1), (2, 1), (1, 8), (1, 6), (1, 7)]>]    
#
#    
#    


if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testExtend']
    unittest.main()